package com.szbber.medical.board.ui.fragment.dialog

import android.os.Bundle
import android.view.View
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.szbber.commen.base.BaseVMFragment
import com.szbber.commen.bean.MessageListBean
import com.szbber.commen.dialog.DialogUtils
import com.szbber.medical.board.R
import com.szbber.medical.board.databinding.*
import com.szbber.medical.board.viewModel.MessageInfoViewModel
import com.szbber.medical.board.viewModel.OrderInfoViewModel
import kotlinx.android.synthetic.main.board_fragment_message_info.*

class MessageInfoFragment : BaseVMFragment<BoardFragmentMessageInfoBinding, MessageInfoViewModel>(),
    View.OnClickListener {

    companion object {
        fun getInstance(cureNo: String?): MessageInfoFragment {
            val sf = MessageInfoFragment()
            sf.cureNo = cureNo
            return sf
        }
    }

    var cureNo: String? = null

    override fun getLayoutResId(): Int = R.layout.board_fragment_message_info

    override fun initVM(): MessageInfoViewModel = ViewModelProvider(this,
        ViewModelProvider.NewInstanceFactory()).get(MessageInfoViewModel::class.java)

    override fun initView(savedInstanceState: Bundle?) {
        mBinding.messageinfoVm = mViewModel
        tvAdd.setOnClickListener(this)
        mViewModel.getMessageList(activity, cureNo)
        mViewModel.messageBean.observe(this) {
            observeRecyclerView(it)
        }
    }

    private fun observeRecyclerView(messageListBean: MessageListBean){

        val adapter = object :BaseQuickAdapter<MessageListBean.Data, BaseViewHolder>(
            R.layout.board_item_message_list, messageListBean.data.toMutableList()
        ){
            override fun convert(holder: BaseViewHolder, item: MessageListBean.Data) {
                val binding =
                    DataBindingUtil.bind<ViewDataBinding>(holder.itemView) as BoardItemMessageListBinding
                    binding.messageVM = item
            }
        }
        mRecyclerViewLeft.adapter  = adapter
        val manager = LinearLayoutManager(activity)
        mRecyclerViewLeft.layoutManager = manager
    }

    override fun onClick(p0: View?) {
        when (p0?.id) {
            R.id.tvAdd -> {
                DialogUtils.showAddMessage(activity) {
                    val data = it.addmessageBean
                    data?.patientId = cureNo
                    mViewModel.addMessage(activity, data)
                }
            }
        }
    }

}